#!/usr/bin/env bash

# Before checking all the env vars, detect whether secrets, usually encrypted, are available or not.
# Secrets are not available when building a pull request, so the script will not check for those.
detect_secrets() {
    SECRETS_AVAILABLE="true"
    if [[ "$TRAVIS_PULL_REQUEST" != "" && "$TRAVIS_PULL_REQUEST" != "false" ]]; then
        SECRETS_AVAILABLE="false"
        echo "Warning: secrets and encrypted variables are not available when testing pull requests."
    fi
}

detect_secrets

if [[ -z "$PCS_STORAGEADAPTER_WEBSERVICE_URL" ]]; then
    echo "Error: the PCS_STORAGEADAPTER_WEBSERVICE_URL environment variable is not defined."
    exit -1
fi

if [[ -z "$PCS_DEVICESIMULATION_WEBSERVICE_URL" ]]; then
    echo "Error: the PCS_DEVICESIMULATION_WEBSERVICE_URL environment variable is not defined."
    exit -1
fi

if [[ -z "$PCS_TELEMETRY_WEBSERVICE_URL" ]]; then
    echo "Error: the PCS_TELEMETRY_WEBSERVICE_URL environment variable is not defined."
    exit -1
fi

if [[ -z "$PCS_AZUREMAPS_KEY" && "$SECRETS_AVAILABLE" = "true" ]]; then
    echo "Error: the PCS_AZUREMAPS_KEY environment variable is not defined."
    exit -1
fi

if [[ -z "$PCS_AUTH_WEBSERVICE_URL" ]]; then
    echo "Error: the PCS_AUTH_WEBSERVICE_URL environment variable is not defined."
    exit -1
fi

# Optional environment variables
if [[ -z "$PCS_OFFICE365_CONNECTION_URL" ]]; then
    echo "Warning: the PCS_OFFICE365_CONNECTION_URL environment variable is not defined."
fi

if [[ -z "$PCS_SOLUTION_NAME" ]]; then
    echo "Warning: the PCS_SOLUTION_NAME environment variable is not defined."
fi

if [[ -z "$PCS_SUBSCRIPTION_ID" ]]; then
    echo "Warning: the PCS_SUBSCRIPTION_ID environment variable is not defined."
fi

if [[ -z "$PCS_ARM_ENDPOINT_URL" ]]; then
    echo "Warning: the PCS_ARM_ENDPOINT_URL environment variable is not defined."
fi
